package com.igoal.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.igoal.entity.DataStatus;
import com.igoal.entity.RandomNews;

@Repository("randomNewsDAO")
public class RandomNewsDAOImpl extends GenericDaoHibernate<RandomNews, Long>
		implements RandomNewsDAO {

	public RandomNewsDAOImpl() {
		super(RandomNews.class);
	}
	
	public RandomNews getRandomNews(){
		Date today = new Date();
		DetachedCriteria dc = DetachedCriteria.forClass(RandomNews.class);						
		dc.add(Restrictions.lt("beginDate", today));
		dc.add(Restrictions.gt("endDate", today));
		getRandomEntities(dc);
		getByStatus(dc, DataStatus.USING);
		
		List<RandomNews> list = findByCriteria(dc, 0, 1);
		
		if(!list.isEmpty()){
			return list.get(0);
		}
		
		return null;
	}
}
